package com.stone.business.sbqxjl.action;

/**
 * <pre>
 * 设备缺陷记录
 * @author
 * </pre>
 */

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.sbqxjl.pojo.TSbqxjl;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.service.DictionaryService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseListAction;
import frm.ssh.util.SpringUtil;

public class SbqxjlListAction extends BaseListAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private ZbjlCommonService myService;
	private int gqdd_total;
	private int gqxq_total;
	private int gqwxc_total;
	private int bhbdd_total;
	private int bhbxq_total;
	private int bhbwxc_total;
	private int jxbdd_total;
	private int jxbxq_total;
	private int jxbwxc_total;
	private int byqbdd_total;
	private int byqbxq_total;
	private int byqbwxc_total;
	private int gqfs_total;
	private int gqjjxc_total;
	private int gqjjfs_total;
	private int gqzdxc_total;
	private int gqzdfs_total;
	private int gqxc_total;
	private int gqyl_total;
	private int bhbfs_total;
	private int bhbjjxc_total;
	private int bhbjjfs_total;
	private int bhbzdxc_total;
	private int bhbzdfs_total;
	private int bhbxc_total;
	private int bhbyl_total;
	private int jxbfs_total;
	private int jxbjjxc_total;
	private int jxbjjfs_total;
	private int jxbzdxc_total;
	private int jxbzdfs_total;
	private int jxbxc_total;
	private int jxbyl_total;
	private int byqbfs_total;
	private int byqbjjxc_total;
	private int byqbjjfs_total;
	private int byqbzdxc_total;
	private int byqbzdfs_total;
	private int byqbxc_total;
	private int byqbyl_total;
	private int gqxqts_total;//工区消缺天数
	private int gqqxs_total;//工区缺陷个数(待计划中的缺陷不统计)
	private int bhbxqts_total;
	private int bhbqxs_total;
	private int jxbxqts_total;
	private int jxbqxs_total;
	private int byqbxqts_total;
	private int byqbqxs_total;
	public ZbjlCommonService getMyService() {
		return myService;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.service = myService;
		this.myService = myService;
	}
	
    /** * 构造方法. */
    public SbqxjlListAction() {
        this.entityClass = TSbqxjl.class;
    }
    
	// 设备类型
	private List<TDictionary> sblxList;
	
	// 缺陷类别
	private List<TDictionary> qxlbList;
	
	//消缺班组
	private List<TOrgan> bzList;
	
	HashMap<String, Object> m = new HashMap<String, Object>();
	/**
	 * 设置查询条件
	 */
	/*public HashMap<String, Object> setSearchCondition() {
		
		//HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();

		m.put("cols", "new map(s.id as id, s.fxrq as fxrq, s.xcsj as xcsj,s.dyfxrname as dyfxrname, s.qxnr as qxnr, s.qxly as qxly,s.bdzname as bdzname, s.xcyy as xcyy, dic.itemName as qxlbname,"
		+ "(select count(*) from TFile f where f.tableid = s.id and f.colname ='TAB_SBQXJL-QXZP') as filecount)");
//		// 登录者的organCode 100001
	String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
//		// 是否管理员
		String isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
//		// 查询条件OUID很重要
		String conditions ="";
		if (!"1".equals(isAdmin)&&!roles.startsWith("GQLD")) {
			conditions += " s.ouid like '%," + organCode + "%,%'";
	} else {
		conditions += " 1=1 ";
		}
//		by wangtao update20110315
		//String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
	//	String isLike = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_ISLIKE));
//		 查询条件OUID很重要
		//String conditions = " s.ouid "+isLike+" '%," + organCode + "%,%'";
	//	String conditions = " s.ouid like "+" '%," + organCode + "%,%'";
		 conditions += " and s.qxlb = dic.itemId and dic.itemType = 'QXLB' and state = '" + super.getSearchBean().getCon1() + "'"; 
		// 状态
			if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())) {
				conditions += " AND s.state = ?";
				valsList.add(super.getSearchBean().getCon1());
			}
		// 发现时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(s.fxrq,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(s.fxrq,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
		// 变电站
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.bdzname like ?";
			valsList.add("%" + super.getSearchBean().getCon4()+ "%");
		}
		
		// 缺陷类别
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
			conditions += " AND s.qxlb = ?";
			valsList.add(super.getSearchBean().getCon7());
		}
		// 变电站 by wangtao add20120223
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.bdzname in (";
			StringBuffer buf= new StringBuffer(""); 
			String[] bdznames = ((String) super.getSearchBean().getCon4() == null? "" : super.getSearchBean().getCon4()).split(",");
			
			for (int i=0; i< bdznames.length; i++) { 
			  if (i!=0) buf.append(", "); 
			  buf.append("?"); 
			  valsList.add("" + bdznames[i]+ "");
			  //System.out.println("bdznames["+i+"]=="+bdznames[i]);
			} 
			buf.append(")"); 
			conditions +=buf;

			
		}
		// 查询条件
		m.put("conditions",conditions);
		m.put("clause", " order by s.fxrq Desc,s.bdzname");
		m.put("alias", "s, TDictionary dic");//, TOrgan o
		m.put("vals", valsList.toArray());
		
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		sblxList = dicService.findList("SBLX");
		
		// 缺陷类别
		qxlbList = dicService.findList("QXLB");
		
		return m;
		
	}*/
	/**
	 * 生成csv
	 */
	@Override
	public HashMap<String, Object> createCsvMap() {
		this.setSearchCondition();
		m.put("title", "设备缺陷记录");
		m.put("searchcols", "serialno,reporttime,bdzname,qxwz,qxnr,qxlbname,resolvetime,xqbzname,xcrname,xqcyname,resolvecontent,sjxqqk,ylwt");
		m.put("colscn", "缺陷编号,发现日期,变电站,设备名称 ,缺陷内容,缺陷类别,消缺时间,消缺班组 ,消缺负责人,消缺成员,消缺情况,实际消缺情况,遗留问题");
		m.put("colsen", new String[] {"serialno","reporttime","bdzname","qxwz","qxnr","qxlbname","resolvetime","xqbzname","xcrname","xqcyname","resolvecontent","sjxqqk","ylwt"});
		m.put("classname", TSbqxjl.class.getName() + " s, TDictionary dic");
		
		return m;
	}
	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		String sql = ""; 
		
		List<Object> valsList = new ArrayList<Object>();
		/*if(super.getSearchBean().getCon1().equals("1")){
            sql="select s.id as id,a.serial_no as serialno,s.sbjg,s.jsrname,s.qxwz,s.sjxqqk,s.ylwt,s.xqcyname,case when (case when TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj, 'YYYY-MM-DD') else  TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') end)<TO_CHAR(sysdate,'YYYY-MM-DD') then 'pink' else '' end as color,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,s.xqbzname,s.phbzname,case when TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj,'YYYY-MM-DD HH24:MI') else TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD HH24:MI') end as jhxcsj,s.hbrname,s.xcrname,s.dwryname as dwry,a.removed as remove,a.category as category,get_bdzname(a.address) as bdzname ,s.fxrq as fxrq, s.xcsj as xcsj,s.dyfxrname as dyfxrname, s.qxnr as qxnr, s.qxly as qxly, s.xcyy as xcyy, dic.ITEM_NAME as qxlbname, (select count(*) from tab_file@WANGTAO_DBLINK f where f.tableid = (select id from TAB_SBQXJL@WANGTAO_DBLINK where defectid=a.cid )  and f.colname ='TAB_SBQXJL-QXZP') as filecount from dm_defect a,SYS_DICTIONARY dic,TAB_SBQXJL s";
		}else{
			sql="select s.id as id,a.serial_no as serialno,s.sbjg,s.jsrname,s.qxwz,s.sjxqqk,s.ylwt,s.xqcyname,case when (case when TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj, 'YYYY-MM-DD') else  TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') end)<TO_CHAR(sysdate,'YYYY-MM-DD') then 'pink' else '' end as color,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,s.xqbzname,s.phbzname,case when TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj,'YYYY-MM-DD HH24:MI') else TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD HH24:MI') end as jhxcsj,s.hbrname,s.xcrname,s.dwryname as dwry,a.removed as remove,a.category as category,get_bdzname(a.address) as bdzname ,s.fxrq as fxrq, s.xcsj as xcsj,s.dyfxrname as dyfxrname, s.qxnr as qxnr, s.qxly as qxly, s.xcyy as xcyy, dic.ITEM_NAME as qxlbname, (select sbzp from TAB_SBQXJL@WANGTAO_DBLINK where defectid = a.cid) as filecount from dm_defect a,SYS_DICTIONARY dic,TAB_SBQXJL s";
				
		}*/
		sql="select s.id as id,a.serial_no as serialno,s.sbjg,s.jsrname,s.qxwz,s.sjxqqk,s.ylwt,s.xqcyname,case when (case when TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj, 'YYYY-MM-DD') else  TO_CHAR(s.tzjhxcsj, 'YYYY-MM-DD') end)<TO_CHAR(sysdate,'YYYY-MM-DD') then 'pink' else '' end as color,TO_CHAR(a.resolve_time,'YYYY-MM-DD HH24:MI') as resolvetime, a.RESOLVE_CONTENT as RESOLVECONTENT,TO_CHAR(a.report_time,'YYYY-MM-DD HH24:MI') as reporttime,a.reporter as reporter,a.content as content,s.xqbzname,s.phbzname,case when TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD') is null then TO_CHAR(s.jhxcsj,'YYYY-MM-DD HH24:MI') else TO_CHAR(s.tzjhxcsj,'YYYY-MM-DD HH24:MI') end as jhxcsj,s.hbrname,s.xcrname,s.dwryname as dwry,a.removed as remove,a.category as category,get_bdzname(a.address) as bdzname ,s.fxrq as fxrq, s.xcsj as xcsj,s.dyfxrname as dyfxrname, s.qxnr as qxnr, s.qxly as qxly, s.xcyy as xcyy, dic.ITEM_NAME as qxlbname, (select sbzp from TAB_SBQXJL@WANGTAO_DBLINK where defectid = a.cid) as filecount from dm_defect a,SYS_DICTIONARY dic,TAB_SBQXJL s";
		
				// 登录者的organCode 100001
	String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
//		// 是否管理员
		String isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
//		// 查询条件OUID很重要
		String conditions ="";
		String order="";
		if (!"1".equals(isAdmin) && !roles.startsWith("GQLD") && !roles.contains("ZBZ") ) { 
			conditions += " where s.ouid like '%" + organCode + "%'";
	} else {
		conditions += " where 1=1 ";
		}
//		by wangtao update20110315
		//String organCode = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE));
	//	String isLike = String.valueOf(super.getSession().getAttribute(IConstant.SESSION_ISLIKE));
//		 查询条件OUID很重要
		//String conditions = " s.ouid "+isLike+" '%," + organCode + "%,%'";
	//	String conditions = " s.ouid like "+" '%," + organCode + "%,%'";
		 //conditions += " and a.cid=s.defectid and s.qxlb = dic.ITEM_ID and dic.ITEM_TYPE = 'QXLB' and state = '" + super.getSearchBean().getCon1() + "'"; 
		conditions += " and a.cid=s.defectid and s.qxlb = dic.ITEM_ID and dic.ITEM_TYPE = 'QXLB' "; 
		// 状态
			if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon1())&&StringUtils.isNullOrBlankFull(super.getSearchBean().getCon9())) {
				conditions += " AND s.state = '"+super.getSearchBean().getCon1()+"'";
				if(super.getSearchBean().getCon1().equals("1")){
					order=" order by jhxcsj Desc";
				}else if(super.getSearchBean().getCon1().equals("2")){
					order=" order by a.resolve_time Desc";
				}
				
			}else{//消缺时间在统计范围内不分状态全部统计
				conditions += " AND (s.state = '0' or s.state = '1' or s.state = '2') ";
			}
		// 发现时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		// 消缺日期
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon9())) {
			conditions += " AND TO_CHAR(a.RESOLVE_TIME,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon9()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon10())) {
			conditions += " AND TO_CHAR(a.RESOLVE_TIME,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon10()+"'";
		
		}
		
		// 缺陷类别
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon7())) {
			conditions += " AND s.qxlb = '"+super.getSearchBean().getCon7()+"'";
			
		}
		// 缺陷内容
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon11())) {
			conditions += " AND a.CONTENT like '%"+super.getSearchBean().getCon11()+"%'";
			
		}
		// 设备类型
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon8())) {
			conditions += " AND s.sblx = '"+super.getSearchBean().getCon8()+"'";
			
		}
		// 消缺班组
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon19())) {
			conditions += " AND s.XQBZ = '"+super.getSearchBean().getCon19()+"'";
			
		}
		// 变电站 by wangtao add20120223
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
			conditions += " AND s.bdz in (";
			StringBuffer buf= new StringBuffer(""); 
			String[] bdznames = ((String) super.getSearchBean().getCon5() == null? "" : super.getSearchBean().getCon5()).split(",");
			
			for (int i=0; i< bdznames.length; i++) { 
			  if (i!=0) buf.append(", "); 
			  buf.append("" + bdznames[i]+ ""); 
			 
			} 
			buf.append(")"); 
			conditions +=buf;

			
		}
		sql=sql+conditions+order;
		// 查询条件
		m.put("pageflag", "sql");
		//System.out.println(sql);
		//m.put("clause", " order by s.jhxcsj Desc");
		m.put("sql", sql);
		//m.put("forward", "SUCCESS");
		
		DictionaryService dicService = (DictionaryService) SpringUtil.getInstance().getBean("dicService");
		//设备类型
		sblxList = dicService.findList("SBLX");
		
		// 缺陷类别
		qxlbList = dicService.findList("QXLB");
		
		//消缺班组
		String bzsql = "from TOrgan where id <=24 and id >=21 ";
		bzList = dicService.findListByHql(bzsql);
		return m;
		
	}
	//未消除缺陷统计 
	public String wxcqxtj(){

		String sql = "";
		String conditions1 = " ";
		// 发现时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions1 += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions1 += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		String conditions2 = " ";
		// 计划消缺开始时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions2 += " AND TO_CHAR(b.jhxcsj,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions2 += " AND TO_CHAR(b.jhxcsj,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		
		//工区缺陷等待计划数
		sql = "select * from dm_defect a where (notify_department = 1 or notify_department1 = 1) and flag is null   " + conditions1;
		gqdd_total = getCount(sql);
		
		//工区消缺计划条数
		sql = "select * from dm_defect a,TAB_SBQXJL b where state=1 and a.cid=b.defectid and jhxcsj is not null  " + conditions2;
		gqxq_total = getCount(sql);
		
		//工区未处理缺陷条数
		gqwxc_total = gqdd_total + gqxq_total;
		
		//保护班缺陷等待计划数
		sql = "select * from dm_defect a,TAB_SBQXJL b where b.state=0 and b.xqbz='21' and a.cid=b.defectid and a.flag is null   " + conditions1;
		bhbdd_total = getCount(sql);
		
		//保护班消缺计划条数
		sql = "select * from dm_defect a,TAB_SBQXJL b where state=1 and a.cid=b.defectid and jhxcsj is not null and xqbz='21'  " + conditions2;
		bhbxq_total = getCount(sql);
		
		//保护班未处理缺陷条数
		bhbwxc_total = bhbdd_total + bhbxq_total;
		//检修班缺陷等待计划数
		sql = "select * from dm_defect a,TAB_SBQXJL b where b.state=0 and b.xqbz='22' and a.cid=b.defectid and a.flag is null   " + conditions1;
		jxbdd_total = getCount(sql);
		
		//检修班消缺计划条数
		sql = "select * from dm_defect a,TAB_SBQXJL b where state=1 and a.cid=b.defectid and jhxcsj is not null and xqbz='22'   " + conditions2;
		jxbxq_total = getCount(sql);
		
		//检修班未处理缺陷条数
		jxbwxc_total = jxbdd_total + jxbxq_total;
		
		//变压器班缺陷等待计划数
		sql = "select * from dm_defect a,TAB_SBQXJL b where b.state=0 and b.xqbz='23' and a.cid=b.defectid and a.flag is null   " + conditions1;
		byqbdd_total = getCount(sql);
		
		//变压班消缺计划条数
		sql = "select * from dm_defect a,TAB_SBQXJL b where state=1 and a.cid=b.defectid and jhxcsj is not null and xqbz='23'   " + conditions2;
		byqbxq_total = getCount(sql);
		
		//变压班未处理缺陷条数
		byqbwxc_total = byqbdd_total + byqbxq_total;
		
		
		return "WXC_SUCCESS";
	}
	//缺陷处理情况统计 
	public String clqxtj(){

		String sql = "";
		String conditions1 = " ";
		// 发现时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions1 += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions1 += " AND TO_CHAR(a.report_time,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		String conditions2 = " ";
		// 发现日期
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions2 += " AND TO_CHAR(b.fxrq,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions2 += " AND TO_CHAR(b.fxrq,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		String conditions3 = " ";
		// 消除结束时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions3 += " AND TO_CHAR(b.XCJSSJ,'yyyy-MM-dd') >= '"+super.getSearchBean().getCon2()+"'";
			
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions3 += " AND TO_CHAR(b.XCJSSJ,'yyyy-MM-dd') <= '"+super.getSearchBean().getCon3()+"'";
		
		}
		String conditions4 = " ";
		conditions4 += " and to_char(a.report_time,'yyyy-MM-dd') not between '"+super.getSearchBean().getCon2()+"' and '"+super.getSearchBean().getCon3()+"'";

		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		
		//工区发生缺陷条数数
		sql = "select a.CATEGORY from dm_defect a where (notify_department = 1 or notify_department1 = 1)  " + conditions1;
		gqfs_total = getCount(sql);
		
		//工区紧急缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a where (notify_department = 1 or notify_department1 = 1) and CATEGORY='0' " + conditions1;
		gqjjfs_total = getCount(sql);
		
		//工区紧急缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='0' and b.defectid=a.cid " + conditions1+conditions3;
		gqjjxc_total = getCount(sql);
		//工区重大缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a where (notify_department = 1 or notify_department1 = 1) and CATEGORY='1' " + conditions1;
		gqzdfs_total = getCount(sql);
		
		//工区重大缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='1' and b.defectid=a.cid " + conditions1+conditions3;
		gqzdxc_total = getCount(sql);
		//工区消除遗留缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid " + conditions4+conditions3;
		gqyl_total = getCount(sql);
		//工区消除缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid " +conditions3;
		gqxq_total = getCount(sql);
		//工区消缺天数(消除日期(无消除日期的按照计划消除日期)-发现日期)
		sql = "select sum(ceil((case b.state when '2' then b.xcjssj when '1' then b.jhxcjssj end)-a.report_time)) AS DiffDate from dm_defect a,TAB_SBQXJL b where 1=1 and a.cid=b.defectid and a.flag is not null " + conditions1;
		gqxqts_total = this.getXqts(sql);
		//工区发生缺陷条数数(待计划中的缺陷不统计)
		sql = "select a.CATEGORY from dm_defect a where 1=1 and flag is not null " + conditions1;
		gqqxs_total = getCount(sql);
		//保护班发生缺陷条数数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='21' " + conditions1;
		bhbfs_total = getCount(sql);
		
		//保护班紧急缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='21' and CATEGORY='0' " + conditions1;
		bhbjjfs_total = getCount(sql);
		
		//保护班紧急缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='0' and xqbz='21' and b.defectid=a.cid " + conditions1+conditions3;
		bhbjjxc_total = getCount(sql);
		//保护班重大缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='21' and CATEGORY='1' " + conditions1;
		bhbzdfs_total = getCount(sql);
		
		//保护班重大缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='1' and xqbz='21' and b.defectid=a.cid " + conditions1+conditions3;
		bhbzdxc_total = getCount(sql);
		//保护班消除遗留缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='21' " + conditions4+conditions3;
		bhbyl_total = getCount(sql);
		//保护班消除缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='21' " +conditions3;
		bhbxq_total = getCount(sql);
		//保护班消缺天数(消除日期(无消除日期的按照计划消除日期)-发现日期)
		sql = "select sum(ceil((case b.state when '2' then b.xcjssj when '1' then b.jhxcjssj end)-a.report_time)) AS DiffDate from dm_defect a,TAB_SBQXJL b where 1=1 and xqbz='21' and a.cid=b.defectid and a.flag is not null " + conditions1;
		bhbxqts_total = this.getXqts(sql);
		//保护班发生缺陷条数数(待计划中的缺陷不统计)
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='21' and flag is not null " + conditions1;
		bhbqxs_total = getCount(sql);
		//检修班发生缺陷条数数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='22' " + conditions1;
		jxbfs_total = getCount(sql);
		
		//检修班紧急缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='22' and CATEGORY='0' " + conditions1;
		jxbjjfs_total = getCount(sql);
		
		//检修班紧急缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='0' and xqbz='22' and b.defectid=a.cid " + conditions1+conditions3;
		jxbjjxc_total = getCount(sql);
		//检修班重大缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='22' and CATEGORY='1' " + conditions1;
		jxbzdfs_total = getCount(sql);
		
		//检修班重大缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='1' and xqbz='22' and b.defectid=a.cid " + conditions1+conditions3;
		jxbzdxc_total = getCount(sql);
		//检修班消除遗留缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='22' " + conditions4+conditions3;
		jxbyl_total = getCount(sql);
		//检修班消除缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='22' " +conditions3;
		jxbxq_total = getCount(sql);
		//检修班消缺天数(消除日期(无消除日期的按照计划消除日期)-发现日期)
		sql = "select sum(ceil((case b.state when '2' then b.xcjssj when '1' then b.jhxcjssj end)-a.report_time)) AS DiffDate from dm_defect a,TAB_SBQXJL b where 1=1 and xqbz='22' and a.cid=b.defectid and a.flag is not null " + conditions1;
		jxbxqts_total = this.getXqts(sql);
		//检修班发生缺陷条数数(待计划中的缺陷不统计)
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid  and xqbz='22' and flag is not null " + conditions1;
		jxbqxs_total = getCount(sql);
		//变压器班发生缺陷条数数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='23' " + conditions1;
		byqbfs_total = getCount(sql);
		
		//变压器班紧急缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='23' and CATEGORY='0' " + conditions1;
		byqbjjfs_total = getCount(sql);
		
		//变压器班紧急缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='0' and xqbz='23' and b.defectid=a.cid " + conditions1+conditions3;
		byqbjjxc_total = getCount(sql);
		//变压器班重大缺陷发生条数
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid and xqbz='23' and CATEGORY='1' " + conditions1;
		byqbzdfs_total = getCount(sql);
		
		//变压器班重大缺陷消除条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and a.CATEGORY='1' and xqbz='23' and b.defectid=a.cid " + conditions1+conditions3;
		byqbzdxc_total = getCount(sql);
		//变压器班消除遗留缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='23' " + conditions4+conditions3;
		byqbyl_total = getCount(sql);
		//变压器班消除缺陷条数
		sql = "select a.CATEGORY from dm_defect a,TAB_SBQXJL b where b.state=2 and b.defectid=a.cid and xqbz='23' " +conditions3;
		byqbxq_total = getCount(sql);
		//变压器消缺天数(消除日期(无消除日期的按照计划消除日期)-发现日期)
		sql = "select sum(ceil((case b.state when '2' then b.xcjssj when '1' then b.jhxcjssj end)-a.report_time)) AS DiffDate from dm_defect a,TAB_SBQXJL b where 1=1 and xqbz='23' and a.cid=b.defectid and a.flag is not null " + conditions1;
		byqbxqts_total = this.getXqts(sql);
		//变压器发生缺陷条数数(待计划中的缺陷不统计)
		sql = "select a.CATEGORY from dm_defect a ,TAB_SBQXJL b where  b.defectid=a.cid  and xqbz='23' and flag is not null " + conditions1;
		byqbqxs_total = getCount(sql);
		return "CL_SUCCESS";
	}
	private int getCount(String sql){
		int count = 0;
		List templist = null;
		templist = myService.findListBySql(sql);
		if(templist != null){
			count = templist.size();
		}
		return count;
	}
	private int getXqts(String sql){
		int count = 0;
		List templist = null;
		templist = myService.findListBySql(sql);
		if(templist != null&&templist.size()>0){
			Object obj=((HashMap)templist.get(0)).get("diffdate");
			if(obj!=null){
			count = Integer.parseInt((((HashMap)templist.get(0)).get("diffdate")).toString());
			}
		}
		return count;
	}
	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public List<TDictionary> getQxlbList() {
		return qxlbList;
	}

	public int getGqdd_total() {
		return gqdd_total;
	}

	public void setGqdd_total(int gqddTotal) {
		gqdd_total = gqddTotal;
	}

	public int getGqxq_total() {
		return gqxq_total;
	}

	public void setGqxq_total(int gqxqTotal) {
		gqxq_total = gqxqTotal;
	}

	public int getGqwxc_total() {
		return gqwxc_total;
	}

	public void setGqwxc_total(int gqwxcTotal) {
		gqwxc_total = gqwxcTotal;
	}

	public int getBhbdd_total() {
		return bhbdd_total;
	}

	public void setBhbdd_total(int bhbddTotal) {
		bhbdd_total = bhbddTotal;
	}

	public int getBhbxq_total() {
		return bhbxq_total;
	}

	public void setBhbxq_total(int bhbxqTotal) {
		bhbxq_total = bhbxqTotal;
	}

	public int getBhbwxc_total() {
		return bhbwxc_total;
	}

	public void setBhbwxc_total(int bhbwxcTotal) {
		bhbwxc_total = bhbwxcTotal;
	}

	public int getJxbdd_total() {
		return jxbdd_total;
	}

	public void setJxbdd_total(int jxbddTotal) {
		jxbdd_total = jxbddTotal;
	}

	public int getJxbxq_total() {
		return jxbxq_total;
	}

	public void setJxbxq_total(int jxbxqTotal) {
		jxbxq_total = jxbxqTotal;
	}

	public int getJxbwxc_total() {
		return jxbwxc_total;
	}

	public void setJxbwxc_total(int jxbwxcTotal) {
		jxbwxc_total = jxbwxcTotal;
	}

	public int getByqbdd_total() {
		return byqbdd_total;
	}

	public void setByqbdd_total(int byqbddTotal) {
		byqbdd_total = byqbddTotal;
	}

	public int getByqbxq_total() {
		return byqbxq_total;
	}

	public void setByqbxq_total(int byqbxqTotal) {
		byqbxq_total = byqbxqTotal;
	}

	public int getByqbwxc_total() {
		return byqbwxc_total;
	}

	public void setByqbwxc_total(int byqbwxcTotal) {
		byqbwxc_total = byqbwxcTotal;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public void setSblxList(List<TDictionary> sblxList) {
		this.sblxList = sblxList;
	}

	public void setQxlbList(List<TDictionary> qxlbList) {
		this.qxlbList = qxlbList;
	}

	public int getGqfs_total() {
		return gqfs_total;
	}

	public void setGqfs_total(int gqfsTotal) {
		gqfs_total = gqfsTotal;
	}

	public int getGqjjxc_total() {
		return gqjjxc_total;
	}

	public void setGqjjxc_total(int gqjjxcTotal) {
		gqjjxc_total = gqjjxcTotal;
	}

	public int getGqjjfs_total() {
		return gqjjfs_total;
	}

	public void setGqjjfs_total(int gqjjfsTotal) {
		gqjjfs_total = gqjjfsTotal;
	}

	public int getGqzdxc_total() {
		return gqzdxc_total;
	}

	public void setGqzdxc_total(int gqzdxcTotal) {
		gqzdxc_total = gqzdxcTotal;
	}

	public int getGqzdfs_total() {
		return gqzdfs_total;
	}

	public void setGqzdfs_total(int gqzdfsTotal) {
		gqzdfs_total = gqzdfsTotal;
	}

	public int getGqxc_total() {
		return gqxc_total;
	}

	public void setGqxc_total(int gqxcTotal) {
		gqxc_total = gqxcTotal;
	}

	public int getGqyl_total() {
		return gqyl_total;
	}

	public void setGqyl_total(int gqylTotal) {
		gqyl_total = gqylTotal;
	}

	public int getBhbfs_total() {
		return bhbfs_total;
	}

	public void setBhbfs_total(int bhbfsTotal) {
		bhbfs_total = bhbfsTotal;
	}

	public int getBhbjjxc_total() {
		return bhbjjxc_total;
	}

	public void setBhbjjxc_total(int bhbjjxcTotal) {
		bhbjjxc_total = bhbjjxcTotal;
	}

	public int getBhbjjfs_total() {
		return bhbjjfs_total;
	}

	public void setBhbjjfs_total(int bhbjjfsTotal) {
		bhbjjfs_total = bhbjjfsTotal;
	}

	public int getBhbzdxc_total() {
		return bhbzdxc_total;
	}

	public void setBhbzdxc_total(int bhbzdxcTotal) {
		bhbzdxc_total = bhbzdxcTotal;
	}

	public int getBhbzdfs_total() {
		return bhbzdfs_total;
	}

	public void setBhbzdfs_total(int bhbzdfsTotal) {
		bhbzdfs_total = bhbzdfsTotal;
	}

	public int getBhbxc_total() {
		return bhbxc_total;
	}

	public void setBhbxc_total(int bhbxcTotal) {
		bhbxc_total = bhbxcTotal;
	}

	public int getBhbyl_total() {
		return bhbyl_total;
	}

	public void setBhbyl_total(int bhbylTotal) {
		bhbyl_total = bhbylTotal;
	}

	public int getJxbfs_total() {
		return jxbfs_total;
	}

	public void setJxbfs_total(int jxbfsTotal) {
		jxbfs_total = jxbfsTotal;
	}

	public int getJxbjjxc_total() {
		return jxbjjxc_total;
	}

	public void setJxbjjxc_total(int jxbjjxcTotal) {
		jxbjjxc_total = jxbjjxcTotal;
	}

	public int getJxbjjfs_total() {
		return jxbjjfs_total;
	}

	public void setJxbjjfs_total(int jxbjjfsTotal) {
		jxbjjfs_total = jxbjjfsTotal;
	}

	public int getJxbzdxc_total() {
		return jxbzdxc_total;
	}

	public void setJxbzdxc_total(int jxbzdxcTotal) {
		jxbzdxc_total = jxbzdxcTotal;
	}

	public int getJxbzdfs_total() {
		return jxbzdfs_total;
	}

	public void setJxbzdfs_total(int jxbzdfsTotal) {
		jxbzdfs_total = jxbzdfsTotal;
	}

	public int getJxbxc_total() {
		return jxbxc_total;
	}

	public void setJxbxc_total(int jxbxcTotal) {
		jxbxc_total = jxbxcTotal;
	}

	public int getJxbyl_total() {
		return jxbyl_total;
	}

	public void setJxbyl_total(int jxbylTotal) {
		jxbyl_total = jxbylTotal;
	}

	public int getByqbfs_total() {
		return byqbfs_total;
	}

	public void setByqbfs_total(int byqbfsTotal) {
		byqbfs_total = byqbfsTotal;
	}

	public int getByqbjjxc_total() {
		return byqbjjxc_total;
	}

	public void setByqbjjxc_total(int byqbjjxcTotal) {
		byqbjjxc_total = byqbjjxcTotal;
	}

	public int getByqbjjfs_total() {
		return byqbjjfs_total;
	}

	public void setByqbjjfs_total(int byqbjjfsTotal) {
		byqbjjfs_total = byqbjjfsTotal;
	}

	public int getByqbzdxc_total() {
		return byqbzdxc_total;
	}

	public void setByqbzdxc_total(int byqbzdxcTotal) {
		byqbzdxc_total = byqbzdxcTotal;
	}

	public int getByqbzdfs_total() {
		return byqbzdfs_total;
	}

	public void setByqbzdfs_total(int byqbzdfsTotal) {
		byqbzdfs_total = byqbzdfsTotal;
	}

	public int getByqbxc_total() {
		return byqbxc_total;
	}

	public void setByqbxc_total(int byqbxcTotal) {
		byqbxc_total = byqbxcTotal;
	}

	public int getByqbyl_total() {
		return byqbyl_total;
	}

	public void setByqbyl_total(int byqbylTotal) {
		byqbyl_total = byqbylTotal;
	}

	/**
	 * @return the bzList
	 */
	public List<TOrgan> getBzList() {
		return bzList;
	}

	/**
	 * @param bzList the bzList to set
	 */
	public void setBzList(List<TOrgan> bzList) {
		this.bzList = bzList;
	}

	public int getGqxqts_total() {
		return gqxqts_total;
	}

	public void setGqxqts_total(int gqxqtsTotal) {
		gqxqts_total = gqxqtsTotal;
	}

	public int getGqqxs_total() {
		return gqqxs_total;
	}

	public void setGqqxs_total(int gqqxsTotal) {
		gqqxs_total = gqqxsTotal;
	}

	public int getBhbxqts_total() {
		return bhbxqts_total;
	}

	public void setBhbxqts_total(int bhbxqtsTotal) {
		bhbxqts_total = bhbxqtsTotal;
	}

	public int getBhbqxs_total() {
		return bhbqxs_total;
	}

	public void setBhbqxs_total(int bhbqxsTotal) {
		bhbqxs_total = bhbqxsTotal;
	}

	public int getJxbxqts_total() {
		return jxbxqts_total;
	}

	public void setJxbxqts_total(int jxbxqtsTotal) {
		jxbxqts_total = jxbxqtsTotal;
	}

	public int getJxbqxs_total() {
		return jxbqxs_total;
	}

	public void setJxbqxs_total(int jxbqxsTotal) {
		jxbqxs_total = jxbqxsTotal;
	}

	public int getByqbxqts_total() {
		return byqbxqts_total;
	}

	public void setByqbxqts_total(int byqbxqtsTotal) {
		byqbxqts_total = byqbxqtsTotal;
	}

	public int getByqbqxs_total() {
		return byqbqxs_total;
	}

	public void setByqbqxs_total(int byqbqxsTotal) {
		byqbqxs_total = byqbqxsTotal;
	}
////////////////////////by wangtao add20121121
	 public String createSqlCvs()
     throws Exception
 {
     HashMap m = createCsvMap();
    // List l = service.getListByDHql((String)m.get("searchcols"), (String)m.get("conditions"), (Object[])m.get("vals"), (String)m.get("clause"), (String)m.get("classname"));
     List l = service.findListBySql((String)m.get("sql"));
     StringBuffer text = new StringBuffer();
     String title = (String)m.get("title");
     text.append((new StringBuilder(String.valueOf(title))).append("\r\n").toString());
     text.append((new StringBuilder(String.valueOf((String)m.get("colscn")))).append("\r\n").toString());
     String col[] = (String[])m.get("colsen");
     for(int i = 0; i < l.size(); i++)
     {
         HashMap map = (HashMap)l.get(i);
         for(int j = 0; j < col.length; j++)
         {
             text.append("\"");
             if(map.get(col[j]) == null)
                 text.append("");
             else
             if(map.get(col[j]) instanceof Timestamp)
                 text.append(DateFormatTool.format((Date)map.get(col[j]), true));
             else
             if(map.get(col[j]) instanceof String)
                 text.append(String.valueOf(map.get(col[j])));
             else
             if(map.get(col[j]) instanceof Double)
                 text.append(map.get(col[j]) != null && ((Double)map.get(col[j])).doubleValue() != 0.0D ? formatterDouble.format(map.get(col[j])) : "0.00");
             else
             if(map.get(col[j]) instanceof Long)
                 text.append(map.get(col[j]) != null && ((Long)map.get(col[j])).longValue() != 0L ? formatterInteger.format(map.get(col[j])) : "0");
             else
             if(map.get(col[j]) instanceof Integer)
                 text.append(map.get(col[j]) != null && ((Integer)map.get(col[j])).intValue() != 0 ? formatterInteger.format(map.get(col[j])) : "0");
             else
                 text.append(map.get(col[j]));
             text.append("\",");
         }

         text.append("\r\n");
     }

     returnStream = new ByteArrayInputStream(text.toString().getBytes());
     return "CSV_SUCCESS";
 }
	///////////////////////////////////////////////////////////
	 private InputStream returnStream;
	    private String csvFileName;
	    static DecimalFormat formatterDouble = new DecimalFormat("#,###,###,###,###.##");
	    static DecimalFormat formatterInteger = new DecimalFormat("#,###,###,###,###");
	    public InputStream getReturnStream()
	    {
	        return returnStream;
	    }
}
